package com.di.base.service.Impl;

import com.di.base.feign.AccountClient;
import com.di.base.feign.StorageClient;
import com.di.base.mapper.OrderMapper;
import com.di.base.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

        @Resource
        OrderMapper orderMapper;

        @Resource
        StorageClient storageClient;

        @Resource
        private AccountClient accountClient;


        @GlobalTransactional(name = "create-order", rollbackFor = Exception.class)
        public void createOrder(String userId, String productId, int count, BigDecimal amount) {
            log.info("开始创建订单，用户：{}，商品：{}，数量：{}，金额：{}", userId, productId, count, amount);

            // 1. 保存订单（伪代码，根据你的OrderMapper接口）
            orderMapper.insertOrder(userId, count,productId, amount);

            // 2. 调用库存服务扣减库存
            storageClient.decrease(productId, count);

            // 3. 调用账户服务扣减余额
            accountClient.debitAccount(userId, amount);

            log.info("订单创建完成");
//            throw new RuntimeException();
        }
}
